package com.mail.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mail.dubbo.mapper.SmsCouponMapper;
import com.mail.model.db.SmsCoupon;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author yan
 * @date 2022/06/22 21:05
 **/
@DubboService
public class SmsCouponApiImpl implements SmsCouponApi {
    @Autowired
    private SmsCouponMapper mapper;

    @Override
    public void add(SmsCoupon smsCoupon) {
        mapper.insert(smsCoupon);
    }

    @Override
    public void delete(Long id) {
        mapper.deleteById(id);
    }

    @Override
    public Page<SmsCoupon> list(Long pageNum, Long pageSize, SmsCoupon smsCoupon) {
        Page<SmsCoupon> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsCoupon> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(smsCoupon.getType() != null, SmsCoupon::getType, smsCoupon.getType());
        wrapper.like(smsCoupon.getName() != null, SmsCoupon::getName, smsCoupon.getName());
        mapper.selectPage(page, wrapper);
        return page;
    }

    @Override
    public void update(SmsCoupon smsCoupon) {
        mapper.updateById(smsCoupon);
    }

    @Override
    public SmsCoupon getById(Long id) {
        return mapper.selectById(id);
    }
}
